<?php

/**
* alltosun.com 分类widget category.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-3 下午4:22:26 $
* $Id$
*/

class category_widget
{
    private $res_name = 'goods';
    private $depth = null;   // 分类的层级深度
    private $parent_id = 0;

    /**
     * 获取分类列表
     * @param array() $param
     */
    public function get_list($param = array())
    {
        if (isset($param['res_name']) && $param['res_name']) {
            $this->res_name = $param['res_name'];
        }
        if (isset($param['depth']) && $param['depth']) {
            $this->depth = $param['depth'];
        }
        if (isset($param['parent_id']) && $param['parent_id']) {
            $this->parent_id = $param['parent_id'];
        }
        $filter['res_name'] = $this->res_name;
        $filter['parent_id'] = $this->parent_id;
        $filter['status']   = 1;

        $parent_path = '';
        if ($this->parent_id > 0) {
            $parent_path = _uri('category', $filter['parent_id'], 'path');
        }

        $list = array();
        $list = _model('category')->getList($filter, " ORDER BY concat(path,'-',id) ASC ");
        if ($this->depth) {
            $parent_depth = 0;
            if ($parent_path != '') {
                $parent_depth = count(explode('-', $parent_path));
            }

            foreach ($list as $k=>$v) {
                $curr_depth = count(explode('-', $v['path']));
                if ($curr_depth - $parent_depth > $this->depth) {
                    unset($list[$k]);
                }
            }
        }

        return $list;
    }

    /**
     * 获取分类列表
     * @param array() $param
     */
    public function get_category_list($param = array())
    {
        if (isset($param['res_name']) && $param['res_name']) {
            $this->res_name = $param['res_name'];
        }
        if (isset($param['per_page']) && $param['per_page']) {
            $limit = ' LIMIT 0,'.$param['per_page'];
        }
        $filter['res_name'] = $this->res_name;
        $filter['parent_id'] = $this->parent_id;
        $filter['status']   = 1;

        $list = array();
        $list = _model('category')->getList($filter, " ORDER BY `add_time` ASC ");
        foreach ($list as $k=>$v) {
            $filter['parent_id'] = $v['id'];
            $list[$k]['children'] = _model('category')->getList($filter, " ORDER BY `add_time` ASC ");
        }

        return $list;
    }

    public function get_title_list($param = array())
    {
        $page_no = Request::getParam('page_no', 1);
        if (isset($param['per_page']) && $param['per_page']) {
            $limit = 'LIMIT 0,'.$param['per_page'];
        }
        if (isset($param['res_name']) && $param['res_name']) {
            $filter['res_name'] = $param['res_name'];
        }
        if (isset($param['parent_id']) && $param['parent_id']) {
            $filter['parent_id'] = $param['parent_id'];
        }
        if (isset($param['parent_title']) && $param['parent_title']) {
            $category_filter = array(
                    'status'        => 1,
                    '`title` LIKE ' => '%'.$param['parent_title'].'%',
            );
            $filter['parent_id'] = _uri('category', $category_filter, 'id');
        }
        $filter['status'] = 1;

        $list = array();
        $list = _model('category')->getList($filter, " ORDER BY `add_time` ASC ".$limit);

        return $list;
    }

}
?>